package cn.edu.ujn.youw127.action;

import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import cn.edu.ujn.youw127.model.Xg;
import cn.edu.ujn.youw127.service.IXgService;

public class XgAction extends ActionSupport{
	private IXgService xgService;
	private Xg xg;


public void setXg(Xg xg) {
	this.xg = xg;
}

	public void setKcService(IXgService xgService) {
		this.xgService = xgService;
	}
	
	public String getAllKc() {
		List<Xg> allKcb = xgService.getAllXg();
		Map<String, Object> request = (Map<String, Object>) ActionContext.getContext().get("request");
		request.put("list", allKcb);
		return "success";
		
		
	}
	
	public String deleteKc() {
		xgService.delete(xg.getUsername());
		return "success";
	}
	
	public String addKc() {
		boolean isHad=false;
		List<Xg> allKcb = xgService.getAllXg();
		for(int i=0;i<allKcb.size();i++) {
			if(xg.getId().equals(allKcb.get(i).getId())) {
				isHad=true;
				break;
			}
		}
		if(isHad) {
			return "error";
		}else {
			xgService.save(xg);
			return "ok";
		}
	}
	public String toUpdate() {
		Xg find = xgService.find(xg.getId());
		if(find!=null) {
			Map<String, Object> request = (Map<String, Object>) ActionContext.getContext().get("request");
			request.put("xgInfo", find);
			return "success";
		}else {
			return "error";
		}
	}
	
	public String updateKc() {
		if (xg!=null) {
			xgService.update(xg);
			return "success";
		}else {
			return "error";
		}
		
	}
}
